<template>
  <div class="wbox">
    <div style="margin-top: 30px">
      <div>
        <div v-if="imageUrl" style="margin-bottom: 10px">
          <img :src="imageUrl" alt="Thumbnail" style="width: 100px; height: 100px" />
        </div>
        <a-upload
          :before-upload="beforeUpload"
          :show-upload-list="false"
          :multiple="false"
          :max-count="1"
          :file-list="fileList"
          @change="handleChange"
        >
          <a-button> 点击上传 </a-button>
        </a-upload>
      </div>
    </div>
    <div id="components-form-demo-advanced-search">
      <a-form class="ant-advanced-search-form" :form="form" @submit="handleSearch">
        <a-row :gutter="24">
          <div v-show="editable">
            <a-col v-for="(item, index) in fields" :key="index" :span="8" :style="{ marginBottom: '0px' }">
              <a-form-item :label="item.label">
                <component
                  :is="item.component"
                  v-bind="item.props"
                  v-decorator="[item.name]"
                  :style="{ width: '100%' }"
                />
              </a-form-item>
            </a-col>
          </div>
          <div v-show="!editable">
            <a-col v-for="(item, index) in fieldsno" :key="index" :span="8" :style="{ marginBottom: '0px' }">
              <a-form-item :label="item.label">
                <component
                  :is="item.component"
                  v-bind="item.props"
                  v-decorator="[
                    item.name // 设置初始值
                  ]"
                  :disabled="!editable"
                  :style="{ width: '100%' }"
                />
              </a-form-item>
            </a-col>
          </div>
        </a-row>
        <a-row> </a-row>
      </a-form>
    </div>
    <div>
      <a-button @click="toggleEditable" style="margin-top: 30px">编辑</a-button>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      fileDetails: [
        {
          imgUrl: 'https://static.jeecg.com/upload/test/3a4490d5d1cd495b826e528537a47cc1.jpg',
        },
      ],
      expand: false,
      editable: false,
      form: this.$form.createForm(this, {
        personCode: '1111',
      }),
      fields: [
        {
          name: 'personCode',
          label: '人员编码',
          component: 'a-input',
          props: { placeholder: '请输入人员编码' },
          required: true,
          message: '请输入人员编码',
          // 绑定的值
          initialValue: '',
        },
        {
          name: 'name',
          label: '姓名',
          component: 'a-input',
          props: { placeholder: '请输入姓名' },
          required: true,
          message: '请输入姓名',
          initialValue: '',
        },
        {
          name: 'gender',
          label: '性别',
          component: 'a-select',
          props: {
            options: [
              { value: '1', label: '男' },
              { value: '2', label: '女' },
            ],
          },
          required: true,
          message: '请选择性别',
          initialValue: '1',
        },
        {
          name: 'age',
          label: '年龄',
          component: 'a-input-number',
          props: { placeholder: '请输入年龄' },
          required: true,
          message: '请输入年龄',
          initialValue: '',
        },
        {
          name: 'birthDate',
          label: '出生年月',
          component: 'a-date-picker',
          props: {},
          required: true,
          message: '请选择出生年月',
        },
        {
          name: 'ethnicity',
          label: '民族',
          component: 'a-input',
          props: { placeholder: '请输入民族' },
          required: true,
          message: '请输入民族',
        },
        {
          name: 'idNumber',
          label: '身份证号码',
          component: 'a-input',
          props: { placeholder: '请输入身份证号码' },
          required: true,
          message: '请输入身份证号码',
        },
        {
          name: 'politicalStatus',
          label: '政治面貌',
          component: 'a-input',
          props: { placeholder: '请输入政治面貌' },
          required: true,
          message: '请输入政治面貌',
        },
        {
          name: 'education',
          label: '学校学历专业',
          component: 'a-input',
          props: { placeholder: '请输入学校学历专业' },
          required: true,
          message: '请输入学校学历专业',
        },
        {
          name: 'fullTimeEducation',
          label: '全日制',
          component: 'a-input',
          props: { placeholder: '请输入全日制学历' },
          required: true,
          message: '请输入全日制学历',
        },
        {
          name: 'nonFullTimeEducation',
          label: '非全日制',
          component: 'a-input',
          props: { placeholder: '请输入非全日制学历' },
          required: true,
          message: '请输入非全日制学历',
        },
        {
          name: 'title',
          label: '职称',
          component: 'a-input',
          props: { placeholder: '请输入职称' },
          required: true,
          message: '请输入职称',
        },
        {
          name: 'nativePlace',
          label: '籍贯',
          component: 'a-input',
          props: { placeholder: '请输入籍贯' },
          required: true,
          message: '请输入籍贯',
        },
        {
          name: 'address',
          label: '家庭住址',
          component: 'a-input',
          props: { placeholder: '请输入家庭住址' },
          required: true,
          message: '请输入家庭住址',
        },
        {
          name: 'hukou',
          label: '户籍所在地',
          component: 'a-input',
          props: { placeholder: '请输入户籍所在地' },
          required: true,
          message: '请输入户籍所在地',
        },
        {
          name: 'maritalStatus',
          label: '婚姻状况',
          component: 'a-select',
          props: {
            options: [
              { value: '1', label: '已婚' },
              { value: '2', label: '未婚' },
            ],
            initialValue: '2',
          },
          required: true,
          message: '请选择婚姻状况',
        },
        {
          name: 'phoneNumber',
          label: '电话号码',
          component: 'a-input',
          props: { placeholder: '请输入电话号码' },
          required: true,
          message: '请输入电话号码',
        },
        {
          name: 'shortNumber',
          label: '短号',
          component: 'a-input',
          props: { placeholder: '请输入短号' },
          required: true,
          message: '请输入短号',
        },
        {
          name: 'vehicleInfo',
          label: '车辆情况',
          component: 'a-input',
          props: { placeholder: '请输入车辆情况' },
          required: true,
          message: '请输入车辆情况',
        },
        {
          name: 'recruitmentMethod',
          label: '招录方式',
          component: 'a-input',
          props: { placeholder: '请输入招录方式' },
          required: true,
          message: '请输入招录方式',
        },
        //  { name: 'militaryService', label: '服役情况', component: 'a-upload', props: {}, required: true, message: '请上传服役情况' },
        //  { name: 'qualificationCertificate', label: '资格证书', component: 'a-upload', props: {}, required: true, message: '请上传资格证书' },
      ],
      fieldsno: [
        { name: 'personCode', label: '人员编码', props: { placeholder: '请输入人员编码' }, message: '请输入人员编码' },
        { name: 'name', label: '姓名', props: { placeholder: '请输入姓名' }, message: '请输入姓名' },
        {
          name: 'gender',
          label: '性别',
          props: {
            options: [
              { value: 'male', label: '男' },
              { value: 'female', label: '女' },
            ],
          },
          message: '请选择性别',
        },
        { name: 'age', label: '年龄', props: { placeholder: '请输入年龄' }, message: '请输入年龄' },
        { name: 'birthDate', label: '出生年月', props: {}, message: '请选择出生年月' },
        { name: 'ethnicity', label: '民族', props: { placeholder: '请输入民族' }, message: '请输入民族' },
        {
          name: 'idNumber',
          label: '身份证号码',
          props: { placeholder: '请输入身份证号码' },
          message: '请输入身份证号码',
        },
        {
          name: 'politicalStatus',
          label: '政治面貌',
          props: { placeholder: '请输入政治面貌' },
          message: '请输入政治面貌',
        },
        {
          name: 'education',
          label: '学校学历专业',
          props: { placeholder: '请输入学校学历专业' },
          message: '请输入学校学历专业',
        },
        {
          name: 'fullTimeEducation',
          label: '全日制',
          props: { placeholder: '请输入全日制学历' },
          message: '请输入全日制学历',
        },
        {
          name: 'nonFullTimeEducation',
          label: '非全日制',
          props: { placeholder: '请输入非全日制学历' },
          message: '请输入非全日制学历',
        },
        { name: 'title', label: '职称', props: { placeholder: '请输入职称' }, message: '请输入职称' },
        { name: 'nativePlace', label: '籍贯', props: { placeholder: '请输入籍贯' }, message: '请输入籍贯' },
        { name: 'address', label: '家庭住址', props: { placeholder: '请输入家庭住址' }, message: '请输入家庭住址' },
        { name: 'hukou', label: '户籍所在地', props: { placeholder: '请输入户籍所在地' }, message: '请输入户籍所在地' },
        {
          name: 'maritalStatus',
          label: '婚姻状况',
          props: {
            options: [
              { value: '1', label: '已婚' },
              { value: '2', label: '未婚' },
            ],
          },
          message: '请选择婚姻状况',
        },
        { name: 'phoneNumber', label: '电话号码', props: { placeholder: '请输入电话号码' }, message: '请输入电话号码' },
        { name: 'shortNumber', label: '短号', props: { placeholder: '请输入短号' }, message: '请输入短号' },
        { name: 'vehicleInfo', label: '车辆情况', props: { placeholder: '请输入车辆情况' }, message: '请输入车辆情况' },
        {
          name: 'recruitmentMethod',
          label: '招录方式',
          props: { placeholder: '请输入招录方式' },
          message: '请输入招录方式',
        },
      ],
      url: {
        importExcelUrl: 'sys/user/importExcel',
      },
      imageUrl: 'https://static.jeecg.com/upload/test/3a4490d5d1cd495b826e528537a47cc1.jpg',
      fileList: [],
      listType: 'picture-card',
    }
  },
  computed: {
    count() {
      return this.expand ? 11 : 7
    },
  },
  updated() {
    console.log('updated')
  },
  methods: {
    handleSearch(e) {
      e.preventDefault()
      this.form.validateFields((error, values) => {
        console.log('error', error)
        console.log('Received values of form: ', values)
      })
    },

    handleReset() {
      this.form.resetFields()
    },

    toggle() {
      this.expand = !this.expand
    },
    toggleEditable() {
      this.editable = !this.editable
    },
    beforeUpload(file) {
      const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'
      if (!isJpgOrPng) {
        this.$message.error('只能上传 JPG 或 PNG 图片!')
      }
      const isLt2M = file.size / 1024 / 1024 < 2
      // if (!isLt2M) {
      //   this.$message.error('图片大小不能超过 2MB!');
      // }
      return isJpgOrPng
    },
    handleChange(info) {
      if (info.file.status === 'done') {
        // 上传成功后，获取图片 URL
        this.imageUrl = info.file.response.url
      } else if (info.file.status === 'error') {
        this.$message.error('上传失败')
      } else {
        // 选择图片后立即显示缩略图
        this.imageUrl = URL.createObjectURL(info.file.originFileObj)
      }
      this.fileList = info.fileList
    },
  },
}
</script>
<style>
.wbox {
  display: flex;
}
.ant-advanced-search-form {
  padding: 24px;
  /* background: #fbfbfb;
  border: 1px solid #d9d9d9; */
  border-radius: 6px;
}

.ant-advanced-search-form .ant-form-item {
  display: flex;
}

.ant-advanced-search-form .ant-form-item-control-wrapper {
  flex: 1;
}

#components-form-demo-advanced-search .ant-form {
  max-width: none;
}
#components-form-demo-advanced-search .search-result-list {
  margin-top: 16px;
  border: 1px dashed #e9e9e9;
  border-radius: 6px;
  background-color: #fafafa;
  text-align: center;
  padding-top: 80px;
}
/* 样式可以根据需要调整 */
img {
  display: block;
  margin: 0 auto;
}
</style>